﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace BestClient
{
    public class SettingsProvider
    {
        private static SettingsProvider _settingsProvider;
        private static Hashtable settings = new Hashtable();

        private const string SNStr = "SN";
        private const string TABLE_CREATE = "TABLE_CREATE";
        public static SettingsProvider getInstance()
        {
            _settingsProvider = new SettingsProvider();
            if (settings.Count <= 0)
            {
                InitSettings("329916131112");
            }
            return _settingsProvider;
        }

        public string get(string key, string defaultValue)
        {
            if (settings.ContainsKey(key))
            {
                return (string)settings[key];
            }

            return defaultValue;
        }


        public int get(string key, int defaultValue)
        {
            int ret = -1;
            if (settings.ContainsKey(key))
            {
                try
                {
                    ret = int.Parse((string)settings[key]);
                }catch(Exception ex){
                    ret = -1;
                }
                return ret;
            }
            return defaultValue;            
        }

        public void set(string key, string keyValue)
        {
            if (settings.Contains(key))
            {
                settings[key] = keyValue;
            }
            else
            {
                settings.Add(key, keyValue);
            }
        }

        public void set(string key, bool keyValue)
        {
            if (settings.Contains(key))
            {
                settings[key] = keyValue;
            }
            else
            {
                settings.Add(key, keyValue);
            }
        }

        public void set(string key, int keyValue)
        {
            set(key, keyValue.ToString());
        }

        public void set(string key, object keyValue)
        {
            if (settings.ContainsKey(key)) {
                settings[key] = keyValue;
            }
            else
            {
                settings.Add(key, keyValue);
            }            
        }

        public void Remove(string key)
        {
            settings.Remove(key);
        }


        public string getDeviceSN()
        {
            return (string)settings[SNStr];
        }

        public static void InitSettings(string devSN)
        {
            settings.Add(SNStr, devSN);
            settings.Add(TABLE_CREATE, "1");
        }

        public string getPersonSyncSize()
        {
            return "10";
        }

        public string getCommandSyncSize()
        {
            return "10";
        }

        public string getHttpUrl()
        {
            return @"http://192.168.221.46:8011/apiv2/device/active";
            //return @"http://device.zkfaceid.com:8011/apiv2/device/active";
            //return @"http://218.17.43.234:8011/apiv2/device/active";
        }

        public string getBestCheckInterval()
        {
            return "500";
        }

        public string getDownloadPersonPath()
        {
            return get("download-all-data-url", "/sdcard/config/downloadPerson");
        }

    }
}
